<template>
  <a-drawer v-model:open="open" :mask-closable="false" class="custom-class" root-class-name="root-class-name"
    width="30%" :title="props.type == 'add' ? '新增运动' : '编辑运动'" placement="right" @close="closebtn">
    <a-form ref="formRef" :model="formState.info" :rules="rules" layout="vertical">
      <a-form-item ref="cover" label="封面" name="cover">
        <uploadImg v-model:value="formState.info.cover" />
      </a-form-item>
      <a-form-item ref="name" label="名称" name="name">
        <a-input v-model:value="formState.info.name" />
      </a-form-item>
      <a-form-item ref="categoryId" label="分类" name="categoryId">
        <a-cascader v-model:value="formState.info.categoryId" multiple style="width: 100%" max-tag-count="responsive"
          :options="optinoslist" placeholder="请选择分类"></a-cascader>
      </a-form-item>
      <a-form-item ref="kilocaloriePerHour" label="每小时消耗的卡路里(千卡)" name="kilocaloriePerHour">
        <a-input-number id="inputNumber" style="width:100%" v-model:value="formState.info.kilocaloriePerHour"
          :min="1" />
      </a-form-item>

      <a-form-item ref="description" label="描述" name="description">
        <a-textarea v-model:value="formState.info.description" placeholder="请输入描述" :auto-size="{ minRows: 5, }" />
      </a-form-item>
    </a-form>
    <template #footer>
      <div style="display: flex; justify-content: flex-end">
        <a-button style="margin-right: 8px" @click="closebtn">取消</a-button>
        <a-button type="primary" :loading="loading" @click="infobtn">确定</a-button>
      </div>
    </template>
  </a-drawer>
</template>

<script setup lang="ts">
import { computed, reactive, ref, watch, inject } from "vue";

import { message } from "ant-design-vue";

import * as api from "#/api/core/sport";
import uploadImg from '#/components/uploadImg/index.vue'


  ;

const props = defineProps({
  value: {
    type: Boolean,
    default: false,
  },
  type: {
    type: String,
    default: "add",
  },
  editdata: {
    type: Object,
    default: () => { },
  },
  catelist: {
    type: Array,
    default: () => [],

  }
});

const emits = defineEmits(["update:value", "submit", "close"]);


const optinoslist = computed({
  get() {
    return props.catelist
  }
})



const formRef = ref();
const rules = {
  name: [
    {
      required: true,
      message: "请输入名称",
      trigger: "change",
    },
  ],
  categoryId: [
    {
      required: false,
      message: "请选择分类",
      trigger: "change",
    },
  ],
};

const formState = reactive({
  info: {
    categoryId: [],
    name: "",
    cover: '',
    kilocaloriePerHour: null,
    description: '',
  },
});

const loading = ref(false);

watch(
  () => props.value,
  (newV) => {
    console.log("props", props.editdata);

    if (newV && props.type == "update") {

      formState.info.categoryId = props.editdata.categoryId
      formState.info.name = props.editdata.name
      formState.info.cover = props.editdata.cover
      formState.info.kilocaloriePerHour = props.editdata.kilocaloriePerHour
      formState.info.description = props.editdata.description

      console.log('回显');


    } else {
      formState.info.categoryId = []
      formState.info.name = ''
      formState.info.cover = ''
      formState.info.kilocaloriePerHour = null
      formState.info.description = ''

    }
  },
  {
    immediate: true,
  }
);

const open = computed({
  get() {
    return props.value;
  },
  set(val) {
    emits("update:value", val);
  },
});



const infobtn = () => {
  console.log("info", formState.info);




  let info = {
    ...formState.info,

  }

  console.log('提交', info);

  formRef.value
    .validate()
    .then(async () => {
      loading.value = true;
      try {
        if (props.type == "add") {
          await api.$createsport({
            ...info
          });
          message.success("添加成功");

          open.value = false;
          loading.value = false;
          emits("submit");
        } else {
          await api.$updatesport(
            {
              ...info
            },
            props.editdata?.id
          );
          message.success("编辑成功");
          loading.value = false;

          open.value = false;
          emits("submit");
        }
        // debugger
      } catch (error) {
        console.log("error", error);

        loading.value = false;

      }
    })
    .catch((error: any) => {
      console.log("error", error);
    });
};

const closebtn = () => {
  open.value = false;
  // formState.info.name = "";
  // formState.info.description = "";
  // formState.info.icon = "";
  emits("close");
};
</script>
